home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Programming / AMOSList / AMOSLIST / text0044.txt < prev    next >
Encoding:
Text File  |  1998-04-01  |  3.8 KB  |  127 lines

  1. > I'm making a dungeon game its levels are 50x50 stored in an array
  2. > I need some code to rotate this map 90 degrees in either direction..
  3.  
  4.    For the sake of simplicity, the code below assumes you're useing a
  5.    two-dimensional array to store your map.  Obviously, a one-
  6.    dimensional array or particularly a binary array would be more
  7.    effiecient, but you can apply the design to whatever method you're
  8.    actually using...
  9.  
  10.    Okay, the idea is simple enough.  To rotate your map counter clock
  11.    wise, you need to transfer the map rows (top to bottom) to the map
  12.    cols (left to right)...
  13.    ABCDE                       EJOTY
  14.    FGHIJ                        DINSX
  15.    KLMNO                      CHMRW
  16.    PQRST                      BGLQV
  17.    UVWXY                     AFKPU
  18.  
  19.    And for clockwise rotation, you simply transfer the map rows (top to
  20.    bottom) to the map cols (right to left)...
  21.    ABCDE                      UPKFA
  22.    FGHIJ                       VQLGB
  23.    KLMNO                     WRMHC
  24.    PQRST                      XSNID
  25.    UVWXY                     YTOJE
  26.  
  27.    Of course, that is simply the way I am viewing it, you could look 
  28.    at the process in a number of ways, but moving on to the code...:)
  29.  
  30.    BTW- I'm on my PC and just did a quick test in C (these routines\
  31.              work fine), so if I make some syntax errors in the AMOS
  32.              version (created on the fly below), you'll know why...
  33.  
  34.   I'm going to use a 5x5 matrix for this example, as my creation code
  35.   fills a 5x5 MAP with the characters in the above examples...
  36.   just change the MAPWIDTH and MAPHEIGHT values to 50.
  37.  
  38.   --------<CODE STARTS HERE>--------  
  39.   Dim MAP(50,100) : Rem Double-height BUFFERED map...
  40.   Shared MAP(), MAPBASE, MAPWIDTH, MAPHEIGHT
  41.  
  42.   Procedure ROTATEMAPLEFT
  43.     DESTBASE=MAPHEIGHT-MAPBASE
  44.     DESTCOL=0
  45.     For REP=0 To MAPHEIGHT-1
  46.       SOURCEROW=MAPBASE+REP
  47.       DESTROW=DESTBASE+(MAPHEIGHT-1)
  48.       For SOURCECOL=0 To MAPWIDTH-1
  49.         MAP(DESTCOL,DESTROW)=MAP(SOURCECOL,SOURCEROW)
  50.         Dec DESTROW
  51.       Next SOURCECOL
  52.       Inc DESTCOL
  53.     Next REP
  54.     MAPBASE=DESTBASE  
  55.   End Proc
  56.  
  57.  Procedure ROTATEMAPRIGHT
  58.     DESTBASE=MAPHEIGHT-MAPBASE
  59.     DESTCOL=MAPWIDTH-1
  60.     For REP=0 To MAPHEIGHT-1
  61.       SOURCEROW=MAPBASE+REP
  62.       DESTROW=DESTBASE
  63.       For SOURCECOL=0 To MAPWIDTH-1
  64.         MAP(DESTCOL,DESTROW)=MAP(SOURCECOL,SOURCEROW)
  65.         Inc DESTROW
  66.       Next SOURCECOL
  67.       Dec DESTCOL
  68.     Next REP
  69.     MAPBASE=DESTBASE  
  70.   End Proc
  71.  
  72.   Procedure MAPVIEW
  73.     For YPOS=0 To MAPHEIGHT-1
  74.       Print "          ";
  75.       For XPOS=0 To MAPWIDTH-1
  76.         Print Chr$(MAP(XPOS,MAPBASE+YPOS));
  77.       Next XPOS
  78.       Print
  79.     Next YPOS
  80.   End Proc 
  81.  
  82.   ' ----------------------------
  83.   ' MAIN CODE HERE
  84.   '-----------------------------
  85.   MAPBASE=0 : Rem Toggled between 0 and 5 for buffering
  86.   MAPWIDTH=5 : MAPHEIGHT=5
  87.  
  88.   Print "Initializing Map..." : PRINT
  89.   TILE=Asc("A")
  90.   For YPOS=0 To MAPHEIGHT-1
  91.     For XPOS=0 To MAPWIDTH-1
  92.       MAP(XPOS,MAPBASE+YPOS)=TILE 
  93.       Add TILE,1,Asc("A") To Asc("Z")
  94.     Next XPOS
  95.   Next YPOS
  96.  
  97.   Print "Map defined as follows:"
  98.   MAPVIEW
  99.  
  100.   Print "Original Map rotated CounterClockWise:"
  101.   ROTATEMAPLEFT
  102.   MAPVIEW
  103.  
  104.   Print "Original Map rotated ClockWise:"
  105.   ROTATEMAPRIGHT : ROTATEMAPRIGHT
  106.   MAPVIEW
  107.  
  108.   Wait Key
  109.   End
  110.   --------<CODE ENDS HERE>--------
  111.   
  112.   Everytime you call these rotation functions, the MAPBASE is 
  113.   changed, so you'll need to use MAPBASE+ROW to access your map.
  114.   I used a buffered design as this is faster than having to copy your
  115.   MAP into a temporary workbuffer then rotate "back" to the MAP
  116.   array...
  117.  
  118.   Anyway, hope this helps... :-)
  119.  
  120.  
  121.  
  122.                 Garfield Benjamin    e-mail:gbenjam@sosbbs.com
  123.                     VerticalShooter(PC): 52% complete(...23 days)
  124.       NEW!! (DemoVersion0.52 available on my GAMES-page) NEW!!
  125.         Website( http://www.sosbbs.com/~gbenjam ): 50% Complete
  126.  
  127.